﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MarioArena.World;

namespace MarioArena.Characters
{
    class HealthBar
    {
        public const int Width = 50;
        public const int Height = 8;
        private const int MarginBottom = 10;
        private const int MarginTop = 20;

        /// <summary>
        /// Margin of the background rectangle with respect to the border rectangle. 
        /// Basically this is also the thickness of the border of the health bar.
        /// </summary>
        private const int MarginBackground = 2;

        private const int BackgroundWidth = Width - (MarginBackground * 2);
        private const int BackgroundHeight = Height - (MarginBackground * 2);

        private Character mCharacter;

        /// <summary>
        /// Border of the healthbar.
        /// </summary>
        private Rectangle mBorder;
        /// <summary>
        /// Background of the healthbar, which is drawn above the border.
        /// </summary>
        private Rectangle mBackground;
        /// <summary>
        /// The life bar itself, drawn on top of the background.
        /// </summary>
        private Rectangle mLifeBar;

        private Texture2D mDummyTexture;

        private bool mUsedByPlayer;

        public HealthBar(Character character, bool usedByPlayer)
        {
            this.mCharacter = character;
            this.mUsedByPlayer = usedByPlayer;

            // Dummy texture
            this.mDummyTexture = new Texture2D(GameManager.GraphicsDevice, 1, 1);
            this.mDummyTexture.SetData(new Color[] { Color.White });
        }

        /// <summary>
        /// Updates the health bar.
        /// </summary>
        public void Update()
        {
            if (this.mUsedByPlayer)
            {
                // Draw the border which contains the background
                int screenPosX = (int)this.mCharacter.Position.X + (this.mCharacter.Width / 2) - (Width / 2);
                int screenPosY = (int)this.mCharacter.Position.Y - this.mCharacter.Height - Height + MarginTop;
                this.mBorder = new Rectangle(screenPosX, screenPosY, Width, Height);
            }
            else
            {
                int screenPosX = (int)(this.mCharacter.CameraX - (Width / 2) + GameManager.Instance.HalfScreenWidth - Camera.X);
                int screenPosY = (int)(this.mCharacter.CameraY - this.mCharacter.Height - this.mCharacter.MarginBottom - Height + MarginTop + GameManager.Instance.HalfScreenHeight - Camera.Y);
                this.mBorder = new Rectangle(screenPosX, screenPosY, Width, Height);
            }

            // Draw the background which contains the life bar
            this.mBackground = new Rectangle(mBorder.X + MarginBackground, mBorder.Y + MarginBackground,
                BackgroundWidth, BackgroundHeight);

            // Draw the life bar depending on the current health
            int lifebarWidth = (int)((BackgroundWidth / (float)this.mCharacter.MaxHealth) * (float)this.mCharacter.Health);
            this.mLifeBar = new Rectangle(mBackground.X, mBackground.Y, lifebarWidth, BackgroundHeight);
        }

        /// <summary>
        /// Draws the health bar.
        /// </summary>
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(this.mDummyTexture, this.mBorder, Color.Black);
            spriteBatch.Draw(this.mDummyTexture, this.mBackground, Color.Gray);

            // Change the color of the life bar depending on the current health
            Color lifeColor = Color.Green;
            if (this.mCharacter.Health < 50)
            {
                if (this.mCharacter.Health > 20)
                    lifeColor = Color.Orange;
                else lifeColor = Color.Red;
            }

            spriteBatch.Draw(this.mDummyTexture, this.mLifeBar, lifeColor);
        }
    }
}
